package com.inspur.attendance.Service.ServiceImpl;

import com.inspur.attendance.Dao.OverTimeDao;
import com.inspur.attendance.Entity.Overtime;
import com.inspur.attendance.Service.OverTimeService;
import com.inspur.attendance.Tools.TimeConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OverTimeServiceImpl implements OverTimeService {

    @Autowired
    OverTimeDao overTimeDao;

    /**
     * 查询全部
     * @return
     * @throws Exception
     */
    public Page<Overtime> getAllOverTime(int pageNo, int pageSize) throws Exception{
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by("id").descending());
        return overTimeDao.findAll(pageable);
    }

    @Override
    public Page<Overtime> getMyOverTime(int pageNo, int pageSize, String id) throws Exception {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by("id").descending());
        return overTimeDao.getMyOverTime(id, pageable);
    }

    /**
     * 按照id查询
     * @param id
     * @return
     * @throws Exception
     */
    public Overtime findById(Long id) throws Exception{
        return overTimeDao.findTopById(id);
    }

    /**
     *
     * @param overtime
     * @throws Exception
     */
    @Transactional
    @Modifying
    @Override
    public void addOverTime(Overtime overtime) throws Exception {
        overTimeDao.save(overtime);
    }

    @Override
    @Transactional
    public void delOverTime(Long id) throws Exception {
        overTimeDao.deleteById(id);
    }

    @Override
    public List<Overtime> MyOvertimeRequest(String id) {
        return overTimeDao.MyOvertimeRequest(id, new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    }

    @Override
    public List<Overtime> overtimeRequest(String departmentId) {
        return overTimeDao.findByDepartment(departmentId, new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    }

}
